/*
 * Licensed by the Bunie Developmentgroup
 */
package gui;

import Protocol.PacketFactory;
import Protocol.PacketTypeKeeper;
import Protocol.QueryRequestPacket;
import java.util.ArrayList;
import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTable;

/**
 * Displays management information in a table. 
 * @author Joep Kemperman
 */
public class TablePanel extends JPanel implements ManagementPanel {
	
	private JTable table;
	private Object[] columnNames;
	private ArrayList<String[]> info;
	
	/**
	 * Constructor
	 */
	public TablePanel() {
		super();
		BoxLayout layout = new BoxLayout(this, BoxLayout.Y_AXIS);
		setLayout(layout);
		add(new JLabel("Please select managementinformation."));
	}
	
	@Override
	public void setManagementInfo(ArrayList<String[]> info) {
		invalidate();
		removeAll();
		this.info = info;
	}
	
	/**
	 * Sets columnnames and creates/displays the table
	 * @param columnNames Column names
	 */
	public void setColumnNames(Object[] columnNames) {
		this.columnNames = columnNames;
		Object[][] data = convertListToData(info);
		table = new JTable(data, columnNames);
		table.setFillsViewportHeight(true);
		table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		add(table.getTableHeader());
		add(table);
		table.setEnabled(false);
		validate();
	}
	
	/**
	 * Conversion from ArrayList to 2D Object array
	 * @param info ArrayList
	 * @return 2D Object array
	 */
	public static Object[][] convertListToData(ArrayList<String[]> info) {
		int columns = info.get(0).length;
		int rows = info.size();
		Object[][] data = new Object[rows][columns];
		
		for (int i=0; i<rows; i++) {
			System.arraycopy(info.get(i), 0, data[i], 0, columns);
		}
		return data;
	}
}
